<?php
	$uri = $_SERVER['REQUEST_URI'];
	$uri_var = explode('/', $uri);
	
	$count = count($uri_var); //echo $count;die();
	$dot = "";
	
	for($i=0; $i<$count-2; $i++)
	{
		$dot = $dot."../";
	}
?>
<?php
	/* Start Process */
	
	require_once($dot."source/global/other/php/recaptcha-php-1.11/recaptchalib.php");
	$privatekey = "6LdJztUSAAAAAFeVw8O68Q1GAx-tylq3hTacq9S8";
	
	$resp = recaptcha_check_answer ($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);
	
	if(!isset($_POST['txt_name'])) {$name = "";} else{$name = sql_quote(trim($_POST['txt_name']));}
	if(!isset($_POST['txt_address'])) {$address = "";} else{$address = sql_quote(trim($_POST['txt_address']));}
	if(!isset($_POST['txt_email'])) {$email = "";} else{$email = sql_quote(trim($_POST['txt_email']));}
	if(!isset($_POST['txt_inquiry'])) {$inquiry = "";} else{$inquiry = sql_quote(trim($_POST['txt_inquiry']));}
	
	//session_complete_destroy();
	session_start();
	$_SESSION['name'] = $name;
	$_SESSION['address'] = $address;
	$_SESSION['email'] = $email;
	$_SESSION['inquiry'] = $inquiry;
	
	$isValid = true;
	$message = "";
	
	// Name
	if ($name == "")
	{
		$isValid = false;
		$message .= "&name=1";
	}
	else if (!ctype_alpha(str_replace(' ', '', $name)))
	{
		$isValid = false;
		$message .= "&name=2";
	}
	
	// Address
	if ($address == "")
	{
		$isValid = false;
		$message .= "&address=1";
	}
	
	// Email
	if ($email == "")
	{
		$isValid = false;
		$message .= "&email=1";
	}
	else if(!filter_var($email, FILTER_VALIDATE_EMAIL))
	{
		$isValid = false;
		$message .= "&email=2";
	}
	
	// Inquiry
	if ($inquiry == "") 
	{
		$isValid = false;
		$message .= "&inquiry=1";
	}
	
	// Captcha
	if (!$resp->is_valid) 
	{
		$isValid = false;
		$message .= "&captcha=1";
	}
	
	if($isValid) 
	{
		/*
		echo "<pre>";
		print_r($_SESSION);
		echo "</pre>";
		*/
		
		// echo "name : ".$name." -<br />";
		// echo "address : ".$address." -<br />";
		// echo "email : ".$email." -<br />";
		// echo "inquiry : ".$inquiry." -<br />";
		// die();
		
		$sql = "insert into tbl_contact_us (contactusname, contactusaddress, contactusemail, contactusmessage, contactusstatus, userin, datein, stsrc) 
				values ('".$name."', '".$address."', '".$email."', '".$inquiry."', '0', '1', '".date("Y-m-d H:i:s")."', 'A')
			";
		//echo $sql;die();
		$exe = mysql_query($sql) or die(mysql_error());
		
		$string_log = "Input Inquiry data by ".$name." ";
		
		addLogByIP($string_log);
		
		$message .= "&success=1";
		
		$_SESSION['valid'] = 1;
		//header("Location: ".getURLdomain()."/contactus/?".($message));
		echo "<meta http-equiv='REFRESH' content='0;url=".getURLdomain()."/contactus/?".($message)."'>";
	}
	else
	{
		$_SESSION['notvalid'] = 1;
		//header("Location: ".getURLdomain()."/contactus/?".($message));
		echo "<meta http-equiv='REFRESH' content='0;url=".getURLdomain()."/contactus/?".($message)."'>";
	}
	
	/* End Process */
?>